package com.gaohe.service.impl;

import com.gaohe.mapper.UserMapper;
import com.gaohe.pojo.User;
import com.gaohe.service.UserService;
import com.gaohe.utils.Code;
import com.gaohe.utils.Message;
import com.gaohe.utils.Result;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper mapper;

    @Override
    public Result login(User user, HttpServletRequest request, HttpServletResponse response) {
        User userFind = mapper.login(user);
        if (Objects.nonNull(userFind)) {
            //登录成功
            //存会话
            request.getSession().setAttribute("user", userFind);
            //记住密码
            Cookie username = new Cookie("username", user.getUsername());
            Cookie password = new Cookie("password", user.getPassword());
            username.setPath("/");
            password.setPath("/");
            if (user.isRemember()) {
                username.setMaxAge(60 * 60 * 24 * 7);
                password.setMaxAge(60 * 60 * 24 * 7);
            } else {
                //必须设置路径才能生效
                username.setMaxAge(0);
                password.setMaxAge(0);
            }
            response.addCookie(username);
            response.addCookie(password);
//            return "success";
            return new Result(Code.GET_SUCCESS, null, Message.SUCCESS);
        }
        return new Result(Code.GET_ERROR, null, Message.ERROR);
    }

    /**
     * 生成随机四位数字验证码
     * @param session
     * @return
     */
    @Override
    public Result getCode(HttpSession session) {
        //生成随机四位数字验证码
        int codeInt = new Random().nextInt(1000, 10000);
        //转换为 字符串
        String code = Integer.toString(codeInt);
        //存入session中
        session.setAttribute("code", code);
        return new Result(Code.GET_CODE_SUCCESS, code, null);
    }

    /**
     * 注册
     *
     * @param user    用户信息
     * @param session 会话
     * @return
     */
    @Override
    public Result register(User user, HttpSession session) {
        //按照用户名查询 重复提示失败
        User userByUsername = mapper.getUserByUsername(user.getUsername());
        if (Objects.nonNull(userByUsername)) {
            return new Result(
                    Code.REGISTER_NAME_ERROR,
                    null,
                    Message.REGISTER_NAME_ERROR
            );
        }
        //验证验证码
        String codeSession = (String) session.getAttribute("code");
        if (!user.getCode().equals(codeSession)) {
            return new Result(
                    Code.REGISTER_CODE_ERROR,
                    null,
                    Message.REGISTER_CODE_ERROR
            );
        }
        //新增
        int res = mapper.savaUser(user);

        return new Result(
                res > 0 ? Code.REGISTER_SUCCESS : Code.REGISTER_BUSINESS_ERROR,
                null,
                res > 0 ? Message.REGISTER_SUCCESS : Message.REGISTER_ERROR
        );
    }

    @Override
    public Result loginOut(HttpSession session) {
        //销毁session
        try {
//            session.removeAttribute("user");
            //销毁
            session.invalidate();
            return new Result(
                    Code.LOGINOUT_SUCCESS,
                    null,
                    Message.LOGINOUT_SUCCESS
            );
        } catch (Exception e) {
            return new Result(
                    Code.LOGINOUT_ERROR,
                    null,
                    Message.LOGINOUT_ERROR
            );
        }
    }

    @Override
    public Result getUserInfo(HttpSession session) {

        User user = (User) session.getAttribute("user");
        return new Result(
                Code.GET_SUCCESS,
                user.getUsername(),
                null
        );
    }


}
